using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;


namespace GameForKids
{

    public class LevelCompleteScreen : DrawableGameComponent
    {
        public event EventHandler Clicked = delegate { };
        // hinh nen
        protected Texture2D blackBackground;
        protected Texture2D background;
        protected Texture2D sao1;
        protected Texture2D sao2;
        protected Texture2D sao3;
        protected float scalesao = 0.8f;
        protected Vector2 posOfBack;
        // cac button
        protected Button replayButton;
        protected Button nextButton;
        protected Button menuButton;
        // tieu de
        protected Texture2D title;
        // am thanh
        protected SoundEffect clicksound;
        private SoundEffect tada;
        // diem so
        public int diem = 0;
        protected SpriteFont font;
        // quan ly hieu ung
        protected Boolean vexong = false;
        protected float timedelay = 0;
        protected float timelimit = 0;
        // hieu ung ngoi sao
        Animation startAnimation1;
        // am thanh
        protected SoundEffect getstar;
        protected int LanPhatThu = 0;

        public bool show { get; set; }

        protected const int TIMEDEPLAY = 700;

        public void showScreen()
        {
            if (diem >= 9)
                timelimit = TIMEDEPLAY*4;
            else if (diem <= 8 && diem >= 7)
                timelimit = TIMEDEPLAY*3;
            else if (diem <= 6 && diem >= 5)
                timelimit = TIMEDEPLAY*2;
            else timelimit = TIMEDEPLAY;
            show = true;
        }

        public void hideScreen()
        {
            vexong = false;
            timedelay = 0;
            timelimit = 0;
            show = false;
        }

        public LevelCompleteScreen(Game game): base(game)
        {
            font = this.Game.Content.Load<SpriteFont>("Fonts/NumberFont");

            blackBackground = this.Game.Content.Load<Texture2D>("MainMenu/Images/black_background");
            background = this.Game.Content.Load<Texture2D>("LevelComplete/Images/background");
            sao1 = this.Game.Content.Load<Texture2D>("LevelComplete/Images/star");
            sao2 = this.Game.Content.Load<Texture2D>("LevelComplete/Images/star");
            sao3 = this.Game.Content.Load<Texture2D>("LevelComplete/Images/star");

            posOfBack = new Vector2((GameForKids.WIDTH - background.Width) / 2, (GameForKids.HEIGHT - background.Height) / 2);
           
            if (GameForKids.language == GameForKids.ANH)
                title = this.Game.Content.Load<Texture2D>("LevelComplete/Images/complete");
            else
                title = this.Game.Content.Load<Texture2D>("LevelComplete/Images/hoanthanh");

            replayButton = new Button(this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonReplay"),
                this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonReplayPress"),
                this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonReplayPress"));
            replayButton.Location = new Point((int)posOfBack.X + 131, (int)posOfBack.Y+355);
            replayButton.Clicked += replayButton_Clicked;

            nextButton = new Button(this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonNext"),
                this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonNextPress"),
                this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonNextPress"));
            nextButton.Location = new Point((int)posOfBack.X + 204, (int)posOfBack.Y + 355);
            nextButton.Clicked += nextButton_Clicked;

            menuButton = new Button(this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonMenu"),
                this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonMenuPress"),
                this.Game.Content.Load<Texture2D>("LevelComplete/Images/buttonMenuPress"));
            menuButton.Location = new Point((int)posOfBack.X + 54, (int)posOfBack.Y + 355);
            menuButton.Clicked += menuButton_Clicked;

            startAnimation1 = new Animation(this.Game.Content.Load<Texture2D>("LevelComplete/Images/StartSheet"), 
                new Point(200, 200), 
                new Point(8,1));
            startAnimation1.PlayFromFrameIndex(0);

            getstar = this.Game.Content.Load<SoundEffect>("LevelComplete/Sounds/GetStar");
            tada = this.Game.Content.Load<SoundEffect>("MemoryGame/Sounds/tada");
            clicksound = this.Game.Content.Load<SoundEffect>("Login/Sounds/button_click");
        }

        void menuButton_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                clicksound.Play();
            LevelCompleteClickState.clickstate = LevelCompleteClickState.ClickState.menu;
        }

        void nextButton_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                clicksound.Play();
            LevelCompleteClickState.clickstate = LevelCompleteClickState.ClickState.next;
        }

        void replayButton_Clicked(object sender, EventArgs e)
        {
            if (GameForKids.ISMUSIC)
                clicksound.Play();
            LevelCompleteClickState.clickstate = LevelCompleteClickState.ClickState.replay;
        }

        public virtual void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.Milliseconds;
            if (!vexong)
                timedelay += elapsed;
            else
                startAnimation1.PlayFromFrameIndex(0);
            startAnimation1.Update();
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (show)
            {
                if (LanPhatThu == 0 && !vexong)
                {
                    if(GameForKids.ISMUSIC)
                    tada.Play();
                    LanPhatThu = 1;
                }
                // ve nen den
                spriteBatch.Draw(blackBackground, new Rectangle(0, 0, GameForKids.WIDTH, GameForKids.HEIGHT), new Color(255, 255, 255, 120));
               // ve popup
                spriteBatch.Draw(background, new Rectangle(
                    (int)posOfBack.X, (int)posOfBack.Y, background.Width, background.Height), Color.White);
                // ve title
                spriteBatch.Draw(title,
                   new Rectangle((int)posOfBack.X + ((background.Width - title.Width) / 2), (int)posOfBack.Y +30, title.Width, title.Height),
                   Color.White);
                // ve sao 1
                if (timedelay >= TIMEDEPLAY && diem >= 5)
                {
                    if (LanPhatThu == 1 && !vexong)
                    {
                        if (GameForKids.ISMUSIC)
                        getstar.Play();
                        LanPhatThu = 2;
                    }
                    spriteBatch.Draw(sao1,
                      new Vector2(posOfBack.X + 65, posOfBack.Y + 170),
                      new Rectangle(0, 0, (int)(sao1.Width), (int)(sao1.Height)),
                      Color.White,
                      -0.2f,
                      new Vector2(sao1.Width / 2, sao1.Height / 2),
                      scalesao,
                      SpriteEffects.None,
                      0);

                    if(timedelay<=TIMEDEPLAY+300)
                    startAnimation1.Draw(spriteBatch,
                       new Vector2(posOfBack.X-35,posOfBack.Y+70),
                       SpriteEffects.None,
                       1.0f,
                       255);
                }
                //ve sao 2
                if (timedelay >= TIMEDEPLAY*2 && diem >= 7)
                {
                    if (LanPhatThu == 2 && !vexong)
                    {
                        if (GameForKids.ISMUSIC)
                        getstar.Play();
                        LanPhatThu = 3;
                    }
                    spriteBatch.Draw(sao2,
                       new Vector2(posOfBack.X + 100, posOfBack.Y + 90),
                       new Rectangle(0, 0, (int)(sao2.Width), (int)(sao2.Height)),
                       Color.White,
                       0,
                       Vector2.Zero,
                       1.0f,
                       SpriteEffects.None,
                       0);

                    if (timedelay <= (TIMEDEPLAY * 2)+300)
                        startAnimation1.Draw(spriteBatch,
                           new Vector2(posOfBack.X + 55, posOfBack.Y + 40),
                           SpriteEffects.None,
                           1.0f,
                           255);
                }
                //ve sao 3
                if (timedelay >= TIMEDEPLAY*3 && diem >= 9)
                {
                    if (LanPhatThu == 3 && !vexong)
                    {
                        if (GameForKids.ISMUSIC)
                        getstar.Play();
                        LanPhatThu = 4;
                    }
                    spriteBatch.Draw(sao3,
                          new Vector2(posOfBack.X + 245, posOfBack.Y + 170),
                          new Rectangle(0, 0, (int)(sao3.Width), (int)(sao3.Height)),
                          Color.White,
                          0.2f,
                          new Vector2(sao1.Width / 2, sao1.Height / 2),
                          scalesao,
                          SpriteEffects.None,
                          0);

                    if (timedelay <= (TIMEDEPLAY * 3)+300)
                        startAnimation1.Draw(spriteBatch,
                           new Vector2(posOfBack.X +145, posOfBack.Y + 70),
                           SpriteEffects.None,
                           1.0f,
                           255);
                }
                // ve diem
                if (timedelay >= timelimit)
                {
                    if (!vexong)
                    {
                        if(GameForKids.ISMUSIC)
                        getstar.Play();
                        LanPhatThu = 0;
                    }
                    spriteBatch.DrawString(font, diem.ToString(),
                        new Vector2(posOfBack.X + (background.Width - font.MeasureString(diem.ToString()).X) / 2,
                            (GameForKids.HEIGHT - font.MeasureString(diem.ToString()).Y) / 2),
                        Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                    vexong = true;
                }
                // ve cac buton
                nextButton.Draw(spriteBatch);
                replayButton.Draw(spriteBatch);
                menuButton.Draw(spriteBatch);
            }
        }

        public void TouchEvent(TouchLocation touch)
        {
            nextButton.TouchEvent(touch);
            menuButton.TouchEvent(touch);
            replayButton.TouchEvent(touch);
        }

       
    }
}
